package com.baofu.paycore.manager.model.withdraw;

import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.common.exception.ValidateException;
import com.baofu.paycore.manager.model.PayBaseBO;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 提现主单
 *
 * User: xin deng  Date: 16/6/3 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Setter
@Getter
@ToString(callSuper = true)
public class WithdrawBO extends PayBaseBO {

    /**
     * 账户号
     */
    private String accountNo;

    /**
     * 业务类型
     */
    private String bizType;

    /**
     * 业务操作类型
     */
    private String subBizType;

    /**
     * 原币币种
     */
    private String sourceCcy;

    /**
     * 原币金额
     */
    private Long sourceAmt;

    /**
     * 汇率
     */
    private BigDecimal rate;

    /**
     * 扩展信息
     */
    private Map<String,String> paymentExt;

    /**
     * 提现资金信息
     */
    private List<WithdrawFundBO> withdrawFundBO;

    /**
     * 提现银行单
     */
    private WithdrawBankBO withdrawBankBO;

    /**
     * 银行名称
     */
    private String bankName;

    /**
     * 外部跳转URL
     */
    private String outPayUrl;

    /**
     * 客户账号
     */
    private String contractNo;

    /**
     * 处理时间
     */
    private Date respDate;

    /**
     * 日志跟踪号
     */
    private String tradeLogId;

    /**
     * 银行响应参数
     */
    private Map<String,String> params;

    /**
     * 商户号
     */
    private String merchantNo;

    /**
     * 服务类型编号
     */
    private String serviceCode;

    /**
     * 柜台号
     */
    private String counterNo;

    /**
     * 银行编码
     */
    private String bankCode;

    /**
     * 模型检查
     */
    @Override
    public void consistencyCheck() {
        //解冻提现单
        WithdrawFundBO withdrawFundBO = this.getWithdrawFund(getWithdrawFundBO(),
                FundTypeEnum.UNFREEZE_WITHDRAW.getCode());
        long withdrawAmt = withdrawFundBO.getAmt();
        long bankAmt = withdrawBankBO.getAmt();
        if (withdrawAmt != bankAmt) {
            log.error("金额校验异常提现金额:{},资金单金额:{}", bankAmt, withdrawAmt);
            throw new ValidateException(PayCoreErrorCode.AMT_IS_NOT_IN_CONFORMITY);
        }
    }

    /**
     * 根据资金单类型从资金明细列表中获取对应的资金明细
     *
     * @param list         资金明细列表
     * @param fundType     资金类型
     * @return             资金明细
     */
    public WithdrawFundBO getWithdrawFund(List<WithdrawFundBO> list, String fundType) {

        if (!CollectionUtils.isEmpty(list)) {
            for (WithdrawFundBO each : list) {

                if (StringUtils.equals(fundType, each.getFundType())) {
                    return each;
                }
            }
        }
        log.warn("此类型资金单不存在:{}", fundType);
        return null;
    }
    /**
     * 状态检查
     */
    @Override
    public void checkStatus() {

    }
}
